package com.dpj.edu.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpj.commonutils.Result;
import com.dpj.edu.entity.Teacher;
import com.dpj.edu.entity.vo.TeacherQuery;
import com.dpj.edu.service.TeacherService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-22
 */

@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @GetMapping("/list")
    public Result list(){
        List<Teacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @GetMapping("{page}/{limit}")
    public Result pageList(@PathVariable Long page,@PathVariable Long limit){
        int i=10/0;
        Page<Teacher> teacherPage=new Page<>(page,limit);
        teacherService.page(teacherPage,null);
        long total=teacherPage.getTotal();

        List<Teacher> records = teacherPage.getRecords();
            return Result.ok().data("total",total).data("records",records);
    }
    @PostMapping("/{page}/{limit}")
    public Result pageQuery(@RequestBody TeacherQuery teacherQuery, @PathVariable Long page, @PathVariable Long limit){
        HashMap map=teacherService.pageQuery(teacherQuery,page,limit);
//        Page<Teacher> teacherPage=new Page<>(page,limit);
//        String name=teacherQuery.getName();
//        Integer level = teacherQuery.getLevel();
//        String begin = teacherQuery.getBegin();
//        String end = teacherQuery.getEnd();
//        QueryWrapper queryWrapper=new QueryWrapper();
//        if(!StringUtils.isEmpty(name)){
//            queryWrapper.like("name",name);
//        }
//        if(!StringUtils.isEmpty(level)){
//            queryWrapper.eq("level",level);
//        }
//        if(!StringUtils.isEmpty(begin)){
//            queryWrapper.ge("gmt_create",begin);
//        }
//        if(!StringUtils.isEmpty(end)){
//            queryWrapper.le("gmt_modified",end);
//        }
//        queryWrapper.orderByDesc("gmt_create");
//        teacherService.page(teacherPage, queryWrapper);
//        long total = teacherPage.getTotal();
//        List<Teacher> records = teacherPage.getRecords();
        Long total = (Long) map.get("total");
        List<Teacher> records = (List<Teacher>) map.get("rows");
        return Result.ok().data("total",total).data("rows",records);

    }
    @PostMapping("/insert")
    public Result save(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return  Result.ok();
        }else{
            return Result.error();
        }
    }
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return Result.ok().data("item",teacher);

    }
    @PutMapping("/edit")
    public Result edit(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }


}


